package view.sprite;

import model.Car;
import model.Config;
import model.Street;
import view.ImageLoader;

import com.trolltech.qt.core.QPoint;

public class CarSprite extends BaseSprite{
	
	private int type;
	
	public CarSprite(final Car c,int type) {
		this.type=type;
		currImage = ImageLoader.get().getImage(ImageLoader.CAR +"_"+type+".png" );
		final QPoint position = getMatrixPosition(c);
		this.setPosition(position);
	}
	
	private QPoint getMatrixPosition(final Car c) {
		final int buildSize = Config.nCellsPalace;
		final int road = Config.nCellsRoad;
		
		final Street street = c.getCurPos();
		
		int lastX=0;
		int lastY=0;
		
		int direction = 0; // go left o down
		
		if(street.getStart().getY()==(street.getEnd().getY())){ 		// H street
			final int min = Math.min(street.getStart().getX(), street.getEnd().getX());
			
			
			if(street.getStart().getX()<street.getEnd().getX()){  	//    	dir =  ->
				setImgGoRight();
				direction = 1;
				lastX = min * buildSize +road * (min+1) + c.getCurPosIndex();
			}else{	 												//		dir =  <-
				setImgGoLeft();
				lastX = min * buildSize +road * (min+1) + ((Config.nCellsPalace-1) - c.getCurPosIndex());
			}
			
			lastY = street.getStart().getY()* buildSize +road * (street.getStart().getY()) + direction;
		}else{ 														// V Street
			
			
			final int min = Math.min(street.getStart().getY(), street.getEnd().getY());
			lastY = min * buildSize +road * (min+1) + c.getCurPosIndex();
			
			if(street.getStart().getY()<street.getEnd().getY()){ 		 //    dir =  V
				lastY = min * buildSize +road * (min+1) + c.getCurPosIndex();
				setImgGoDown();
			}
			else{//	   dir =  ^
				lastY = min * buildSize +road * (min+1) + ((Config.nCellsPalace-1) - c.getCurPosIndex());
				setImgGoUp();
				direction=1;
			}
			lastX = street.getStart().getX()* buildSize +road * (street.getStart().getX()) + direction;
		}
				
		if(street.equals(c.getDestPos())&& c.getCurPosIndex()==c.getDestPosIndex()){
			setImgArrived();
		}
		if(c.getCurPos().getStart().equals(c.getDestPos().getEnd()) && c.getCurPos().getEnd().equals(c.getDestPos().getStart()) && c.getCurPosIndex() == Config.nCellsPalace - 1 - c.getDestPosIndex())
		{
			setImgArrived();
		}
		
		return new QPoint(lastX,lastY);
	}

	public void setImgGoUp(){
		currImage = ImageLoader.get().getImage(ImageLoader.CARUP+"_"+type);
//		currImage = currImage.transformed(new QTransform(-1, 0, 0, -1,1,1));  //Reflect
	}
	public void setImgGoDown(){
		currImage = ImageLoader.get().getImage(ImageLoader.CAR +"_"+type+".png" );	}
	
	public void setImgGoLeft(){
		currImage = ImageLoader.get().getImage(ImageLoader.CARLEFT+"_"+type);
//		currImage = currImage.transformed(new QTransform(0, 1, -1, 0,1,1)); //
	}
	public void setImgGoRight(){
		currImage = ImageLoader.get().getImage(ImageLoader.CARRIGHT+"_"+type);
//		currImage = currImage.transformed(new QTransform(0, -1, 1, 0,1,1)); //
	}
	public void setImgArrived(){
		currImage = ImageLoader.get().getImage(ImageLoader.ARRIVE);
//		currImage = currImage.transformed(new QTransform(0, -1, 1, 0,1,1)); //
	}
	
	
	

}
